package com.diaohw.module.metadata.controller.dictionary;

import com.diaohw.module.metadata.obj.dto.DictionaryDTO;
import com.diaohw.module.metadata.obj.query.DictionaryQuery;
import com.diaohw.module.metadata.obj.vo.DictionaryVO;
import com.diaohw.module.metadata.service.DictionaryService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.common.validator.InsertGroup;
import com.diaohw.platform.common.validator.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author D-Hw
 * @since 2024-09-18
 */
@Tag(name = "数据字典")
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Resource
    private DictionaryService service;

    @PostMapping
    @Operation(summary = "新增")
    public DictionaryVO create(@RequestBody @Validated(InsertGroup.class) DictionaryDTO data) {
        return service.create(data);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新")
    public DictionaryVO update(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) DictionaryDTO data) {
        return service.update(id, data);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public Boolean delete(@PathVariable("id") String id) {
        return service.delete(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "依据ID查看信息")
    public DictionaryVO detail(@PathVariable("id") String id) {
        return service.detail(id);
    }

    @Operation(summary = "依据ID集合查询")
    @GetMapping("/batch")
    public List<DictionaryVO> findById(List<String> ids) {
        return DictionaryService.INSTANCE.entityToVo(service.listByIds(ids));
    }

    @GetMapping
    @Operation(summary = "查询列表")
    public List<DictionaryVO> all(DictionaryQuery query) {
        return service.findByList(query);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查看")
    public PageResultVo<DictionaryVO> page(DictionaryQuery query) {
        return service.findByPage(query);
    }

    @GetMapping("/enum/{code}")
    @Operation(summary = "依据CODE查找")
    public DictionaryVO getService(@PathVariable("code") String code) {
        DictionaryQuery query = new DictionaryQuery().setDictionaryCode(code).setChildren(Boolean.TRUE);
        List<DictionaryVO> list = service.findByList(query);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }


}
